/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.fx.client;

import me.l1k3.fx.client.channel.StyleChannel;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Unit;

public class FXDimension extends FXChannel {
    
	//- - - factory
	
    public static FXDimension dimension(Element element, int startWidth, int endWidth, int startHeight, int endHeight) {
        FXDimension dimension = new FXDimension();
        dimension.setWidth(startWidth, endWidth);
        dimension.setHeight(startHeight, endHeight);
        dimension.setHideAndShow((startWidth==0 || startHeight==0), true);
        dimension.setHideAndShow((endWidth==0 || endHeight==0), false);
        dimension.init(element);
        
        return dimension;
    }
	
    public static FXDimension to(Element element, int width, int height) {
        FXDimension dimension = new FXDimension();
        dimension.setWidth(element, width);
        dimension.setHeight(element, height);
        dimension.setHideAndShow((width==0 || height==0), false);
        dimension.init(element);
        
        return dimension;
    }
	
	//- - -
	
    public void to(int width, int height) {
        Element element = getContext().getElement();
        setWidth(element, width);
        setHeight(element, height);
        setHideAndShow((width==0 || height==0), false);
    }
    
	public final void setWidth(Element element, int end) {
		setWidth(element.getOffsetWidth(), end);
	}
	
	public final void setWidth(int start, int end) {		
	    add(StyleChannel.setStyle(getType(), "width", start, end, Unit.PX));
	}
	
	public final void setHeight(Element element, int end) {
		setHeight(element.getOffsetHeight(), end);
	}
	
	public final void setHeight(int start, int end) {
		add(StyleChannel.setStyle(getType(), "height", start, end, Unit.PX));
	}
}